<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<title>画板</title>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
    let canvas = document.getElementById("canvas");
    canvas.width = document.documentElement.clientWidth;
    canvas.height = document.documentElement.clientHeight;

    let ctx = canvas.getContext("2d");
    ctx.fillStyle = "black";
    ctx.strokeStyle = "block";
    ctx.lineWidth = 8;
    ctx.lineCap = "round"

    function drawLine(x1, y1, x2, y2) {
        ctx.beginPath();
        ctx.moveTo(x1, y1);
        ctx.lineTo(x2, y2);
        ctx.stroke();
    }

    let painting = false;

    //检测是否支持触屏
    let isTouchDevice = 'ontouchstart' in document.documentElement;
    if (isTouchDevice) {
        canvas.ontouchstart = ev => {
            let x = ev.touches[0].clientX;
            let y = ev.touches[0].clientY;
            last = [x, y];
        }
        canvas.ontouchmove = e => {
            let x = e.touches[0].clientX;
            let y = e.touches[0].clientY;
            drawLine(last[0], last[1], x, y);
            last = [x, y];
        }
    } else {
        canvas.onmousedown = (e) => {
            painting = true;
            last = [e.clientX, e.clientY]
        }

        canvas.onmouseup = () => {
            painting = false;
        }
        canvas.onmousemove = (e) => {
            if (painting) {
                drawLine(last[0], last[1], e.clientX, e.clientY);
                last = [e.clientX, e.clientY]
            }
        }
    }


</script>
</body>
</html>